package com.fubowen.lepton.ui.reader

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fubowen.lepton.R
import com.fubowen.lepton.databinding.FragmentReaderCatalogBinding
import com.fubowen.lepton.extension.changeColor
import com.fubowen.lepton.helper.DBHelper
import com.fubowen.lepton.helper.ViewHelper.setShapeColor
import com.fubowen.lepton.ui.base.ThemeDialogFragment
import com.fubowen.lepton.ui.reader.recycler.BookCatalogAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

class CatalogFragment : ThemeDialogFragment() {

    private lateinit var binding: FragmentReaderCatalogBinding
    private lateinit var recycler: RecyclerView
    private lateinit var adapter: BookCatalogAdapter
    private lateinit var layoutManager: LinearLayoutManager
    private var lastIndex: Int = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentReaderCatalogBinding.inflate(inflater, container, false)
        recycler = binding.catalogRecycler
        adapter = BookCatalogAdapter(requireContext())
        recycler.adapter = adapter
        layoutManager = recycler.layoutManager as LinearLayoutManager
        initCatalog()
        initCatalogController()
        return binding.root
    }

    companion object {

        private const val PERCENT_HEIGHT = "percentHeight"
        fun newInstance(percentHeight: Float): CatalogFragment {
            return CatalogFragment().apply {
                arguments = bundleOf(PERCENT_HEIGHT to percentHeight)
            }
        }
    }

    //region 初始化目录列表
    private fun initCatalog() {
        CoroutineScope(Dispatchers.Main).launch {
            var catalogJob: Job? = null
            DBHelper.db.bookDao().getReadingBookFlow().collect { book ->
                if (book != null) {
                    binding.catalogName.text = book.name
                    catalogJob?.cancel()
                    catalogJob = launch {
                        DBHelper.db.bookDao().getCatalogFlow(book.id).collect {
                            adapter.submitList(it)
                            lastIndex = it.size - 1
                        }
                    }
                }
            }
        }
    }
    //endregion

    //region 初始化目录控制菜单
    private fun initCatalogController() {
        val color = requireContext().getColor(R.color.reader_2)
        val darkColor = changeColor(color, brightness = 0.90f)
        binding.apply {
            catalogTop.setShapeColor(darkColor)
            catalogBottom.setShapeColor(darkColor)
            catalogCurrent.setShapeColor(darkColor)
            catalogClose.setShapeColor(darkColor)
            catalogTop.setOnClickListener { recycler.scrollToPosition(0) }
            catalogBottom.setOnClickListener { recycler.scrollToPosition(lastIndex) }
            catalogCurrent.setOnClickListener { recycler.scrollToPosition(0) }
            catalogClose.setOnClickListener { dialog?.dismiss() }
        }

        adapter.setOnClickListener(object : BookCatalogAdapter.OnClickListener {
            override fun onItemClick(view: View) {
                val parent = parentFragment
                if (parent is CloseCatalogView) {
                    val position = layoutManager.getPosition(view)
                    parent.jumpChapter(position)
                }
            }
        })
    }
    //endregion

    //region 父级对外提供的控制方法
    interface CloseCatalogView {
        fun close()
        fun jumpChapter(position: Int)
    }

    //endregion
}